package com.tangly.codeing.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池
 *
 * @author tangbing
 * @date 2018/12/13
 */
@Configuration
public class ThreadPoolConfig {

    @Bean(name = "myThreadPoolExecutor")
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程数
        executor.setCorePoolSize(8);
        // 最大线程数
        executor.setMaxPoolSize(192);
        // 队列最大长度
        executor.setQueueCapacity(0);
        // 线程池维护线程所允许的空闲时间，默认为60s
        executor.setKeepAliveSeconds(200);
        // 线程池对拒绝任务（无线程可用）的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setThreadNamePrefix("threadPoolExecutor-");
        executor.initialize();
        return executor;
    }
}